##parameters=assessmentIdentity, actionIdentity, comments=None

from DateTime import DateTime

now = DateTime().strftime("%d %b %Y %H:%M")


username = str( context.REQUEST.AUTHENTICATED_USER )
    
userProviderService = context.knowledgeengine_userproviderservice
user = userProviderService.getUser( username )
             
action = context.getComponentByIdentity( actionIdentity )

ass = context.knowledgeengine_assessmentstorageservice 
assessment = ass.loadAssessment( assessmentIdentity, userIdentity=user.getIdentity())
    
        
# Changing workflow states may remove the assessment from the users personal repository.
# Remember the current user so we can re-add later if needed.
#oldAuthor = context.getIPOwner( assessmentIdentity )

#oldState     = context.getIPWorkflowState( assessmentIdentity )

#newState = None

# First, trigger the action
if actionIdentity:
    
    if not action:
        raise "Unable to find action %s" % actionIdentity

    action.execute( assessment, user )
    
    ass.saveAssessment(assessment)
    
    #newState = context.getIPWorkflowState( assessmentIdentity )
    
    msg = "Executing action %s " % `action.title_or_id()`
    
else:
    action = None
    msg    = "(No Action)"

ass.logWorkflowChange( assessment, action, comments, msg )
    
    
    
redirect = context.REQUEST.RESPONSE.redirect
msg = "Workflow status updated"

return redirect('workflow_form?assessmentIdentity=%s&portal_status_message=%s' % (assessmentIdentity, msg))
